package progress;

import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.layout.*;

// 进度指示器
public class ProgressIndicatorDemo extends Application {
	private Pane root = new Pane();
	private ProgressIndicator pi;
	private ScheduledService<Double> ss;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		pi = new ProgressIndicator(0);
		pi.setPrefSize(100, 100);
		pi.setLayoutX(100);
		pi.setLayoutY(30);
		root.getChildren().add(pi);
		
		Scene scene = new Scene(root, 300, 200);
		stage.setResizable(false);
		stage.setScene(scene);
		stage.show();
		
		// 多任务
		ss = new ScheduledService<Double>() {
					
			double i = 0;
			@Override
			protected Task<Double> createTask() {
						
				Task<Double> task = new Task<Double>() {
					@Override
					protected Double call() throws Exception {	
						return i = i + 0.05;  
					}
							
					@Override
					protected void updateValue(Double value) {   			// JavaFX线程
						pi.setProgress(i); 
						if(i < 0.3) {
							pi.setStyle("-fx-progress-color: red");			// 设置进度指示器的颜色
						} else if(i < 0.6) {
							pi.setStyle("-fx-progress-color: orange");
						} else if(i < 0.8) {
							pi.setStyle("-fx-progress-color: yellow");
						} else if(i >= 1){
							pi.setStyle("-fx-progress-color: lime");
							ss.cancel();
						} 
					}
				}; 
				return task;
			}
		};
				
		ss.setDelay(Duration.millis(0));		// 设置执行的延迟时间
		ss.setPeriod(Duration.millis(200));	// 任务执行的周期
		ss.start();		// 任务开始
	}
}





